const Base = require('./base.js');
module.exports = class extends Base {
	
	constructor(ctx) {
		super(ctx);
		this.payment = this.model('store/payment');
	}
	async indexAction() {
		if(this.isGet) {
			return this.display();
		}

		let posts = this.post();

		let where = {};
		if(!think.isEmpty(posts.paymentid)){
			where.payment_id = posts.paymentid;
		}
		const data = await this.payment.dataTable(posts, where);
		console.log(JSON.stringify(data))
		return this.json(data);
	}
	
	async addAction() {
		if(this.isGet) {

			return this.display();
		}

		let posts = this.post();
		

		let payment_id = await this.payment.add({
			name: posts.name,
			code: posts.code,
			status: posts.disabled ? 0 : 1,
		});
		if(payment_id == 0) {
			return this.fail("添加失败");
		}
		return this.success("添加成功");

	}

	async editAction() {
		if(this.isGet) {
			let id = this.get("id");
			let payment = await this.payment.findOne(id);
			this.assign("payment", payment);
			return this.display();
		}

		let posts = this.post();
		let rows = await this.payment.setOne(posts.id,{
			name: posts.name,
			code: posts.code
		});
		if(rows == 0) {
			return this.fail("修改失败");
		}

		

		return this.success("修改成功");
	}
};